package org.wattdepot.cli.commands;

import org.junit.Test;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;

/**
 * This class tests the sensor time stamp command of the 
 * CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandSensorTimeStamp {
  String cr = System.getProperty("line.separator");
  
  /**
   * Tests the normal operation of the sensorTimeStamp command.
   */
  @Test
  public void testSensorTimeStamp() {
    // Create a new sensor time stamp command object.
    SensorTimeStampCommand command = new SensorTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_2", "2009-10-31T23:45:00.000-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    String knownOutput = "Tool: OscarDataConverter";
    knownOutput = knownOutput.concat(cr);
    knownOutput = knownOutput.concat("Source: SIM_KAHE_2");
    knownOutput = knownOutput.concat(cr);
    knownOutput = knownOutput.concat("Properties: (powerGenerated: 5.7E7) ");
    knownOutput = knownOutput.concat(cr);

    // Compare the two to see if they are the same.
    int result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known sensor with good timestamp", result, 0);

    // Ready the arguments to the next command.
    args = Arrays.asList( "SIM_WAIAU_7", "2009-11-15T02:00:00.000-10:00" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready another output that we know is true
    knownOutput = "Tool: OscarDataConverter";
    knownOutput = knownOutput.concat(cr);
    knownOutput = knownOutput.concat("Source: SIM_WAIAU_7");
    knownOutput = knownOutput.concat(cr);
    knownOutput = knownOutput.concat("Properties: (powerGenerated: 8.8E7) ");
    knownOutput = knownOutput.concat(cr);

    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known sensor with good timestamp", result, 0);
  }

  /**
   * Test bad timestamp detection for the sensorTimeStamp command.
   */
  @Test
  public void testSensorBadTime() {
    // Create a new sensor time stamp command object.
    SensorTimeStampCommand command = new SensorTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_2", "2-10-31T23:45:00.000-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Check to see if the correct error returned
    assertTrue("Checking for correct response to bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));
  }

  /**
   * Test bad source detection for the sensorTimeStamp command.
   */
  @Test
  public void testSensorBadSource() {
    // Create a new sensor time stamp command object.
    SensorTimeStampCommand command = new SensorTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "BLAH", "2009-10-31T23:45:00.000-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Check to see if the right error returned.
    assertTrue("Checking for correct response to bad source", sourceOutput
        .contains(WattDepotCliCommand.resourceNotFound));
  }
  
  /**
   * Tests to see if the SensorTimeStamp command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new sensor time stamp command object.
    SensorTimeStampCommand command = new SensorTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1", "2", "3");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess SensorTimeStamp argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
  
  /**
   * Tests to see if the SensorTimeStamp command can handle not enough arguments.
   */
  @Test
  public void testFewArguments() {
    // Create a new sensor time stamp command object.
    SensorTimeStampCommand command = new SensorTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking SensorTimeStamp handling not enough arguments", 
        sourceOutput.contains(WattDepotCliCommand.missingArguments));
  }
}
